package api

import (
	"encoding/json"
	"fmt"
	"zinx_websokect_poker/zinx/ziface"
	"zinx_websokect_poker/zinx/znet"
	"zinx_websokect_poker/zinx_app/poker_game/core"
	json2 "zinx_websokect_poker/zinx_app/poker_game/json"
	"zinx_websokect_poker/zinx_app/poker_game/log"
	"zinx_websokect_poker/zinx_app/poker_game/protocol"
)

type LeaveRoomApi struct {
	znet.BaseRouter
}
func (*LeaveRoomApi)Handle(request ziface.IRequest) {
	//退出房间路由
	p, _ := request.GetConnection().GetProperty("Pid")
	pid := p.(uint32)
	//获取玩家信息
	o := core.OccupantMgrObj.GetOccupantByPid(pid)

	if o == nil {
		log.Info("LeaveRoom","【退出房间】退出房间失败，无法获取到玩家信息。")
		return
	}

	if r := o.GetRoom(); r!=nil{
		r.LeaveRoom(o)
		fmt.Println("退出房间",r.GetNumber())
	}else {
		leave := &protocol.StandUp{
			Code: 1,
		}
		//没有房间信息，退出失败
		jsonStu := json2.Jsonmr(59, leave)
		log.Info("LeaveRoom","【退出房间】玩家退出房间失败，没有房间信息。" +
			"用户昵称："+o.Nickname)

		o.WriteMsg(jsonStu)
	}

}
type ChatApi struct {
	znet.BaseRouter
}

func (*ChatApi)Handle(request ziface.IRequest) {
	jr :=  json2.Jsrep{}
	data := request.GetData()
	err:=json.Unmarshal(data,&jr)
	//解析失败会报错，如json字符串格式不对，缺"号，缺}等。
	if err!=nil{
		fmt.Println(err)
	}
	//oo, _ := request.GetConnection().GetProperty("Oc")
	//o := oo.(*core.Occupant)
	//o.GetPlayerid()
	p, _ := request.GetConnection().GetProperty("Pid")
	pid := p.(uint32)
	o := core.OccupantMgrObj.GetOccupantByPid(pid)
	m := protocol.Chat{
		Uid: o.Pid,
		Text: jr.Msg.(string),
	}
	if r := o.GetRoom(); r!=nil{
		r.Chat(m)
		fmt.Println("聊天",r.GetNumber())
	}
}

